package project.active;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import model.pojo.Employee;
import project.dao.EmployeeDAO;
import project.dao.ProjectDAO;
import project.model.PrProject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import approve.action.R;

public class Newproject extends Activity {
	EditText namepro,despro,datestartpro,datecompletepro;
	Spinner managerpro;
	Employee emp;
	RadioButton rdSt0,rdSt1;
	Button btUpdate, btDelete, btMenuProjectList, btInsert;
	PrProject pr =new PrProject();
	ProjectDAO prodao=new ProjectDAO();
	EmployeeDAO emdao=new EmployeeDAO();
	Bundle language;
	TextView lbproid, lbproname,lbprodes,lbprods, lbprodc,lbpromana;
	String lang="EN",database;
	TextView logout,username;
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lnewproject);
		language = getIntent().getExtras().getBundle(getLang());
		
		lbproid=(TextView)findViewById(R.id.ln_tvProjectID);
		lbproname=(TextView)findViewById(R.id.ln_tvProjectName);
		lbprodes=(TextView)findViewById(R.id.ln_tvDescription);
		lbprods=(TextView)findViewById(R.id.ln_tvDateStart);
		lbprodc=(TextView)findViewById(R.id.ln_tvDateComplete);
		lbpromana=(TextView)findViewById(R.id.ln_tvManager);
		
		//idpro=(EditText)findViewById(R.id.ln_txtIDProject);
		namepro=(EditText)findViewById(R.id.ln_txtProjectName);
		despro=(EditText)findViewById(R.id.ln_txtDescription);
		datestartpro=(EditText)findViewById(R.id.ln_txtDateStart);
		datecompletepro=(EditText)findViewById(R.id.ln_txtDateCompelete);
		managerpro=(Spinner)findViewById(R.id.ln_cbManager);
		rdSt0=(RadioButton)findViewById(R.id.ln_rdStatus0);
		rdSt1=(RadioButton)findViewById(R.id.ln_rdStatus1);
		
		Date dd=new Date();
		
		lbproid.setText(language.getString("ac_MNLProjectCode")+": "+""+dd.getTime());
		lbproname.setText(language.getString("ac_SUProjectName")+"(*)");
		lbprodes.setText(language.getString("Description"));
		lbprods.setText(language.getString("start_date"));
		lbprodc.setText(language.getString("complete_date"));
		lbpromana.setText(language.getString("ac_Manager"));
		rdSt0.setText(language.getString("notcomplete"));
		rdSt1.setText(language.getString("complete"));
		
		
		ArrayAdapter<Employee> arr = new ArrayAdapter<Employee>(this,android.R.layout.simple_spinner_item,loadManager());
		managerpro.setAdapter(arr);
		
		rdSt0.setChecked(true);
		rdSt1.setChecked(false);
		
		
		
		pr.setIdProject(""+dd.getTime());
		pr.setDateStart(new Date());
		pr.setDateComplete(new Date());
		pr.setStatus(0);
		//idpro.setText(pr.getIdProject());
						
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		datestartpro.setText(sdf.format(pr.getDateStart()));
		datecompletepro.setText(sdf.format(pr.getDateComplete()));
		
		btMenuProjectList=(Button)findViewById(R.id.ln_btUpInfoMenu);
		btInsert=(Button)findViewById(R.id.ln_btInsertProInfo);
		btMenuProjectList.setOnClickListener(goProjectListMenu);
		btInsert.setOnClickListener(insertProjectInfo);
				
		btInsert.setText(language.getString("ac_cus_Insert"));
		btMenuProjectList.setText(language.getString("ac_SUProjectList"));
		
		rdSt0.setOnClickListener(checkSt0);
		rdSt1.setOnClickListener(checkSt1);
		
		Intent i=this.getIntent();
		emp=(Employee)i.getSerializableExtra("em");
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
	}
	
	public ArrayList<Employee> loadManager(){
		ArrayList<Employee> mn=new ArrayList<Employee>();
		EmployeeDAO emdao=new EmployeeDAO();
		mn=emdao.getAllEmployee(getDatabase());
		return mn;
	}
	
	private OnClickListener goProjectListMenu = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			goback();
		}
	};
	
	public void goback(){
//		Intent i= new Intent(LProjectdetail.this,Projectlist.class);
//		i.putExtra("lang", getLang());
//		i.putExtra("database", getDatabase());
//		i.putExtra("em",emp);
//		startActivity(i);
		Intent i= new Intent(Newproject.this,Projectlist.class);
		i.putExtras(getIntent().getExtras());
		i.putExtra("lang", getLang());
		i.putExtra("database", getDatabase());
		i.putExtra("em",emp);
		
		startActivity(i);
	}
	
	private OnClickListener insertProjectInfo = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(!checkNull()){
				pr.setNameProject(namepro.getText().toString());
				pr.setDescription(despro.getText().toString());						
				pr.setIdManager(emdao.getOneEmployeeByName(managerpro.getSelectedItem().toString(),getDatabase()).getIdemployee());
				
				String ds=datestartpro.getText().toString()+" 00:00:00";
				String dc=datecompletepro.getText().toString()+" 00:00:00";
				//System.out.println("dc: "+dc);
				pr.setdS(ds);
				pr.setdC(dc);
				
				if(!prodao.checkExitInsert(pr, getDatabase())){
					if(prodao.insertProjectInfo(pr, getDatabase())>0){
						Intent i= new Intent(Newproject.this,Laddmemmenu.class);
						i.putExtras(getIntent().getExtras());
						i.putExtra("lang", getLang());
						i.putExtra("database", getDatabase());
						i.putExtra("em",emp);
						i.putExtra("project", pr);
						
						startActivity(i);
					}else{
						openDialogThemKhongDuoc();
					}
					
				}else{
					openDialogExit();
				}
			}else{
				openDialogNull();
			}
		}
	};
	
	public boolean checkNull(){
		boolean kq=false;
		if( namepro.getText().toString().equals(null) || datestartpro.getText().toString().equals(null) || datecompletepro.getText().toString().equals(null) ||  namepro.getText().toString().equals("") || datestartpro.getText().toString().equals("") || datecompletepro.getText().toString().equals("")){
			kq=true;
		}		
		return kq;
	}
	
	private OnClickListener checkSt0 =new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(rdSt0.isChecked()){
				rdSt1.setChecked(false);
				pr.setStatus(0);
			}else{
				rdSt1.setChecked(true);
				pr.setStatus(1);
			}
		}
	};
	
	private OnClickListener checkSt1 = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(rdSt1.isChecked()){
				rdSt0.setChecked(false);
				pr.setStatus(1);
			}else{
				rdSt0.setChecked(true);
				pr.setStatus(0);
			}
		}
	};
	
	private void openDialogNull() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(language.getString("infoSys"));// Set tiÃªu Ä‘á»�
		builder.setMessage(language.getString("ac_tb_DataNotBlank"));
		builder.setCancelable(false);// Set cÃ³ cho ngÆ°á»�i dÃ¹ng Cancer báº±ng nÃºt
										// quay láº¡i (back) ko? false: ko
		builder.setPositiveButton(language.getString("yes"), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃ m cÃ¡i gÃ¬ Ä‘Ã³ khi áº¥n Yes táº¡i Ä‘Ã¢y
				dialog.dismiss();
			}
		});
		builder.show();
	}
	
	private void openDialogThemKhongDuoc() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(language.getString("infoSys"));// Set tiÃªu Ä‘á»�
		builder.setMessage(language.getString("st_du_insertFail"));
		builder.setCancelable(false);// Set cÃ³ cho ngÆ°á»�i dÃ¹ng Cancer báº±ng nÃºt
										// quay láº¡i (back) ko? false: ko
		builder.setPositiveButton(language.getString("yes"), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃ m cÃ¡i gÃ¬ Ä‘Ã³ khi áº¥n Yes táº¡i Ä‘Ã¢y
				dialog.dismiss();
			}
		});

		builder.show();
	}
	
	private void openDialogExit() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(language.getString("infoSys"));// Set tiÃªu Ä‘á»�
		builder.setMessage(language.getString("ac_tb_CSDLDaTonTai"));
		builder.setCancelable(false);// Set cÃ³ cho ngÆ°á»�i dÃ¹ng Cancer báº±ng nÃºt
										// quay láº¡i (back) ko? false: ko
		builder.setPositiveButton(language.getString("yes"), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃ m cÃ¡i gÃ¬ Ä‘Ã³ khi áº¥n Yes táº¡i Ä‘Ã¢y
				dialog.dismiss();
			}
		});

		builder.show();
	}
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Newproject.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
}
